package com.skillworld.webapp.model.team;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.skillworld.webapp.model.department.Department;
import com.skillworld.webapp.model.user.User;


@Entity
@Table(name="Team")
public class Team {
	
	private Long teamId;
	private String name;
    private String info;
	private String image;
	private int points;
	private Calendar heldDate;
	private Set<Department> departments;
    private User leader;
	private Set<User> members = new HashSet<User>();
	
	
	protected Team()
	{

	}

    public Team(String name, String info, String image, User leader,
                Set<Department> departments) 
    {
        this.name = name;
        this.info = info;
        this.image = image;
        this.points = 0;
        this.heldDate = Calendar.getInstance(); // "right now"
        this.departments = departments;
        this.leader = leader;
        this.addMember(leader);
    }


    @SequenceGenerator(name="TeamIdGenerator",sequenceName="TeamSeq")
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO,generator="TeamIdGenerator")
    @Column(name="idTeam")
    public Long getTeamId() {
        return teamId;
    }

    public void setTeamId(Long teamId) {
        this.teamId = teamId;
    }

    @Column(name="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="info")
    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
    
    @Column(name="img")
    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    @Column(name="points")
    public int getPoints() {
        return points;
    }

    protected void setPoints(int points) {
        this.points = points;
    }
    
    public void addPoints(int points)
    {
        this.points += points;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="heldDate")
    public Calendar getHeldDate() {
        return heldDate;
    }

    public void setHeldDate(Calendar heldDate) {
        this.heldDate = heldDate;
    }

    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="TeamDept",
            joinColumns=@JoinColumn(name="idTeam"),
            inverseJoinColumns=@JoinColumn(name="idDepartment")
           )
    public Set<Department> getDepartments() {
        return departments;
    }

    protected void setDepartments(Set<Department> departments) {
        this.departments = departments;
    }

    @ManyToOne(optional=false,fetch=FetchType.LAZY)
    @JoinColumn(name="idLeader")
    public User getLeader() {
        return leader;
    }

    public void setLeader(User leader) {
        this.leader = leader;
    }

    @ManyToMany
    @JoinTable(name="UserTeam",
            joinColumns=@JoinColumn(name="idTeam"),
            inverseJoinColumns=@JoinColumn(name="idUser")
           )
    public Set<User> getMembers() {
        return members;
    }

    protected void setMembers(Set<User> members) {
        this.members = members;
    }
    
    public void addMember(User user) {
        this.getMembers().add(user);
        user.getTeams().add(this);
    }
    
    public void removeMember(User user) {
        this.getMembers().remove(user);
        user.getTeams().remove(this);
    }

}
